import { resolve } from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import Icons from 'unplugin-icons/vite'
import IconResolver from 'unplugin-icons/resolver'
import Components from 'unplugin-vue-components/vite'
import AutoImport from 'unplugin-auto-import/vite'
import VueI18n from '@intlify/vite-plugin-vue-i18n'
import WindiCSS from 'vite-plugin-windicss'
import { VitePWA } from 'vite-plugin-pwa'

function pathResolve(dir: string) {
    return resolve(process.cwd(), '.', dir)
}

export default defineConfig({
    resolve: {
        alias: [
            // @/xxxx => src/xxxx
            {
                find: /@\//,
                replacement: pathResolve('src') + '/'
            },
            // #/xxxx => types/xxxx
            {
                find: /#\//,
                replacement: pathResolve('types') + '/'
            }
        ]
    },
    plugins: [
        vue({
            include: [/\.vue$/, /\.md$/]
        }),

        // https://github.com/antfu/unplugin-auto-import
        AutoImport({
            imports: ['vue', 'vue-router', 'vue-i18n', '@vueuse/head', '@vueuse/core'],
            dts: 'types/auto-imports.d.ts'
        }),

        // https://github.com/antfu/unplugin-vue-components
        Components({
            extensions: ['vue', 'md'],
            include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
            resolvers: [
                IconResolver({
                    componentPrefix: ''
                })
            ],
            dts: 'types/auto-components.d.ts'
        }),

        // https://github.com/antfu/unplugin-icons
        Icons({
            autoInstall: true
        }),

        // https://github.com/intlify/bundle-tools/tree/main/packages/vite-plugin-vue-i18n
        VueI18n({
            // 如果您使用 vite 进行生产构建，Vue I18n 将自动捆绑仅运行时模块。如果需要使用 Message 编译器进行按需编译，则需要将 runtimeOnly 选项设置为 false
            runtimeOnly: true,
            // Vue I18n 只能使用 Composition API，如果要使用 Legacy API，则需要将 compositionOnly 选项设置为 false
            compositionOnly: true,
            include: [pathResolve('src/locales/lang/**')]
        }),

        // https://github.com/antfu/vite-plugin-windicss
        WindiCSS(),

        // https://github.com/antfu/vite-plugin-pwa
        VitePWA({
            registerType: 'autoUpdate',
            includeAssets: ['favicon.svg', 'robots.txt', 'safari-pinned-tab.svg'],
            manifest: {
                name: 'Dageek Vite Template',
                short_name: 'dageek',
                theme_color: '#ffffff',
                icons: [
                    {
                        src: '/pwa-192x192.png',
                        sizes: '192x192',
                        type: 'image/png'
                    },
                    {
                        src: '/pwa-512x512.png',
                        sizes: '512x512',
                        type: 'image/png'
                    }
                ]
            }
        })
    ],
    optimizeDeps: {
        include: ['vue', 'vue-router', '@vueuse/core', '@vueuse/head']
    }
})
